package org.social.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.PlayerMapCache;
import org.social.db.proxy.bean.PlayerMapProxy;
import org.social.manager.InterceptManager;
import org.utils.RemoteUtil;

import com.dal.gm_api.api.IAccount;
import com.dal.gm_api.constant.OperateAccountType;
import com.google.common.collect.Lists;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.third.rpc.util.SerializationUtil;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * Created by fxf on 2018-1-5.
 */
@SuppressWarnings("unchecked")
public class AccountQueryServer implements IAccount {

	
	@Override
	public List<String> getAccountInfos(List<String> accountIdList, int channelId) {
		List<String> list = Lists.newArrayList();
		List<Map> accountInfoList = RemoteUtil.loginSync().getAccountInfo(accountIdList, channelId);
		createGMAccountInfo(list, accountInfoList);
		return list;
	}



	private void createGMAccountInfo(List<String> list, List<Map> accountInfoList) {
		accountInfoList.forEach(map -> {
			
			String playerName = "-";
			int totalRechargeMoney = 0;
			int vip = 0;
			//"id","account_id","channel_id","channel_app_id","password","email","device_token","device_name","device_id","sdk","sdk_version","os_name","os_version","phone_number","client_version","question","answer","admin","game_server_id","language","ip","last_online_time","last_offline_time","state","online_state","activate","create_date","modified_date",};
			String uid = MapUtils.getString(map, "id","");
			PlayerMap playerMap = PlayerMapCache.me().getByUid(uid);
			if (playerMap != null) {
				PlayerMapProxy proxy = playerMap.getPlayerMapProxy();
				playerName = proxy.getName();
				totalRechargeMoney = proxy.getTotalRechargeMoney();
				vip = proxy.getVip();
			}
			
			map.put("playerName", playerName);
			map.put("totalRechargeMoney", totalRechargeMoney);
			map.put("vip", vip);
			
			list.add(JsonManager.me().getJson().toJson(map));
		});
	}
	
	

	@Override
	public boolean operateAccount(int type,List<String> uids, Map paramMap) {
		if (type == OperateAccountType.INTERCEPT_TYPE_ACCOUNT_ID || type == OperateAccountType.INTERCEPT_TYPE_MUTE || type == OperateAccountType.INTERCEPT_TYPE_MUTE_SILENT) {
			Date deadline = ToolMap.getDate("deadline", paramMap,DateUtil.offsetHour(new Date(), 6));
			String remarks = ToolMap.getString("remarks", paramMap);
			InterceptManager.addInterceptInfo(type, uids, deadline, remarks);
		}else {
			RemoteUtil.loginSync().operateAccount(type, uids, paramMap);			
		}
		return true;
	}



	@Override
	public List<String> getAccountByPid(int... pids) {
		List<String> uids = Lists.newArrayList();
		for (int pid : pids) {
			PlayerMap playerMap = PlayerMapCache.me().getByPid(pid);
			if (playerMap!=null) {
				uids.add(playerMap.getUid());
			}
		}
		
		List<String> list = Lists.newArrayList();
		List<Map> accountInfoList = RemoteUtil.loginSync().getAccountInfo(uids);
		createGMAccountInfo(list, accountInfoList);
		return list;
	}
}

